package cn.com.shadowless.xinning.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mengpeng.mphelper.ToastUtils;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.adapter.AssetPayAdapter;
import cn.com.shadowless.xinning.adapter.ContractReviewAdapter;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.AssetPayInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;
import cn.com.shadowless.xinning.view.activity.OfficeActivity;

/**
 * The type Asset pay fragment.
 *
 * @author sHadowLess
 */
public class AssetPayFragment extends BaseFragment {

    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_id)
    TextView assetPayId;

    /**
     * The Asset pay tax code.
     */
    @BindView(R.id.asset_pay_taxCode)
    TextView assetPayTaxCode;
    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_createTime)
    TextView assetPayCreateTime;

    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_creater)
    TextView assetPayCreater;

    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_departmentName)
    TextView assetPayDepartmentName;

    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_applier)
    TextView assetPayApplier;

    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_assetNo)
    TextView assetPayAssetNo;
    /**
     * The Fixid.
     */
    @BindView(R.id.fixid)
    LinearLayout fixid;
    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_supply)
    TextView assetPaySupply;

    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_orderNum)
    TextView assetPayOrderNum;

    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_budgeName)
    TextView assetPayBudgeName;

    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_ReimbursementAmount)
    TextView assetPayReimbursementAmount;

    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_notReimbursementAmount)
    TextView assetPayNotReimbursementAmount;

    /**
     * The Contract review remark.
     */
    @BindView(R.id.asset_pay_remark)
    TextView assetPayRemark;

    /**
     * The Asset pay invoice type name.
     */
    @BindView(R.id.asset_pay_invoiceTypeName)
    TextView assetPayInvoiceTypeName;
    /**
     * The Contract review attach info.
     */
    @BindView(R.id.asset_pay_attachInfo)
    RecyclerView assetPayAttachInfo;
    /**
     * The Contract review check process.
     */
    @BindView(R.id.asset_pay_check_process)
    LinearLayout assetPayCheckProcess;

    private final String url = "api/FixedExpandForm/LoadCheckHistory?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_asset_pay_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initAssetPayInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "id") + orderId, mActivity, assetPayCheckProcess);
        }
    }

    @Override
    protected void initView() {

    }

    private void initAssetPayInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getAssetPayInfo(orderId, userName, new RequestCallBack<AssetPayInfo>() {
                    @Override
                    public void success(RequestResult<AssetPayInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.getResult().toString());
                                AssetPayInfo.DataDTO dataBean = result.getResult().getData();
                                List<AssetPayInfo.DataDTO.FileListDTO> fileList = dataBean.getFileList();
                                String taxCode = dataBean.getTaxCode();
                                String id = dataBean.getId();
                                String createTimeStr = dataBean.getCreateTimeStr();
                                String createUserName = dataBean.getCreateUserName();
                                String departmentName = dataBean.getDepartmentName();
                                String applicantName = dataBean.getApplicantName();
                                String fixedApplyId = dataBean.getFixedApplyId();
                                String supplierName = dataBean.getSupplierName();
                                String appendixNum = getDecimal(dataBean.getAppendixNum().doubleValue());
                                String budgeName = dataBean.getBudgetName();
                                String amountWithDuty = getDecimal(dataBean.getAmountWithDuty().doubleValue());
                                String amountWithOutDuty = getDecimal(dataBean.getAmountWithOutDuty().doubleValue());
                                String remark = dataBean.getRemark();
                                String invoiceTypeName = dataBean.getInvoiceTypeName();
                                assetPayTaxCode.setText(taxCode);
                                assetPayId.setText(id);
                                assetPayCreateTime.setText(createTimeStr);
                                assetPayCreater.setText(createUserName);
                                assetPayDepartmentName.setText(departmentName);
                                assetPayApplier.setText(applicantName);
                                if (fixedApplyId != null) {
                                    fixid.setVisibility(View.VISIBLE);
                                    assetPayAssetNo.setText(fixedApplyId);
                                } else {
                                    fixid.setVisibility(View.GONE);
                                }
                                assetPaySupply.setText(supplierName);
                                assetPayOrderNum.setText(appendixNum);
                                assetPayBudgeName.setText(budgeName);
                                assetPayReimbursementAmount.setText(amountWithDuty);
                                assetPayNotReimbursementAmount.setText(amountWithOutDuty);
                                assetPayRemark.setText(remark);
                                assetPayInvoiceTypeName.setText(invoiceTypeName);

                                assetPayAttachInfo.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
                                AssetPayAdapter contractReviewAdapter = new AssetPayAdapter(R.layout.fragment_asset_pay_item, fileList);
                                contractReviewAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                                    String fileName = fileList.get(position).getFileName();
                                    String fileUrl = fileList.get(position).getFileUrl();
                                    Intent intent = new Intent(mActivity, OfficeActivity.class);
                                    intent.putExtra("fileName", fileName);
                                    intent.putExtra("fileUrl", fileUrl);
                                    mActivity.startActivity(intent);
                                });
                                assetPayAttachInfo.setAdapter(contractReviewAdapter);
                                contractReviewAdapter.notifyDataSetChanged();
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("暂无订单列表");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }
}
